//
//  DataHandle.m
//  doubanMovie
//
//  Created by lanou3g on 15/11/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "DataHandle.h"
#import "MovieDetail.h"
#import "DataTool.h"
#import "CinemaModel.h"
@interface DataHandle ()


@end

static DataHandle *handle = nil;

@implementation DataHandle

+ (DataHandle *)sharedInstance
{
    if (handle == nil) {
        handle = [[DataHandle alloc] init];

        }
    return handle;
}
#pragma mark - 获得电影数据
- (void)getDataSourceWithReloadBlack:(ReloadBlock)block
{
    [DataTool requestDataWithURLStr:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php"
                         HTTPMethod:nil
                       withHttpBody:nil
                         blockBlock:^(id object) {
                             NSMutableDictionary *dic = (NSMutableDictionary *)object;
                             self.arrMovieList = [NSMutableArray array];
                                 NSMutableArray *arr = [dic valueForKey:@"result"];
                                 for (NSMutableDictionary *dic1 in arr) {
                                     MovieDetail *model = [[MovieDetail alloc] init];
                                     [model setValuesForKeysWithDictionary:dic1];
                                     [self.arrMovieList addObject:model];
                                 }
                             dispatch_async(dispatch_get_main_queue(), ^{
                                 block();
                             });
                         }];
}


- (void) movieID:(NSString *)movieIDStr ReloadBlack:(ValueBlock)block
{
    [DataTool requestDataWithURLStr:[NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@",movieIDStr]
                         HTTPMethod:nil
                       withHttpBody:nil
                         blockBlock:^(id object) {
                             NSMutableDictionary *dic = (NSMutableDictionary *)object;
                             NSMutableDictionary *dic1 = [dic valueForKey:@"result"];
                             MovieDetail *model = [[MovieDetail alloc] init];
                             [model setValuesForKeysWithDictionary:dic1];
                             [self.arrMovieDetil addObject:model];
                             dispatch_async(dispatch_get_main_queue(), ^{
                                 block(model);
                             });
                             
                         }];
}
#pragma mark - 根据movieID获得电影详细数据
//- (void) loadDetailWithReloadBlack:(ReloadBlock)block
//{
//
//    self.arrMovieDetil = [NSMutableArray array];
//    for (MovieDetail *movie in self.arrMovieList)
//   }
#pragma mark - 获取影院数据
- (void)getCinemaDataSourceReloadBlack:(ReloadBlock)block
{
    self.arrCinema = [NSMutableArray array];
    [DataTool requestDataWithURLStr:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/cinemalist.php" HTTPMethod:nil withHttpBody:nil blockBlock:^(id object) {
        NSMutableDictionary *dic = (NSMutableDictionary *)object;
        NSDictionary *dic1 = dic[@"result"];
        NSMutableArray *arr = dic1[@"data"];
        for (NSDictionary *d in arr) {
            CinemaModel *cinema = [[CinemaModel alloc] init];
            [cinema setValuesForKeysWithDictionary:d];
            [self.arrCinema addObject:cinema];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            block(self.arrCinema);
        });
    }];
}


#pragma mark - 获取电影数量
- (NSInteger)rowOfMovie
{
    return _arrMovieList.count;
}



//获取某个电影详情对象
- (MovieDetail *)movieDetailForIndexPath:(NSIndexPath *)indexPath
{
    return self.arrMovieList[indexPath.row];
}

#pragma mark - 获取影院数量
- (NSInteger)rowOfCinema
{
    return self.arrCinema.count;
}

#pragma mark - 获取某个影院详情对象
- (CinemaModel *)cinemaModerForIndexPath:(NSIndexPath *)indexPath
{
    return self.arrCinema[indexPath.row];
}


@end
